package com.zpxt.zpxt.serve;

import com.zpxt.zpxt.dao.CompanyRepository;
import com.zpxt.zpxt.dao.TalentRepository;
import com.zpxt.zpxt.entity.Company;
import com.zpxt.zpxt.entity.Talent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:hjx
 * @Description:
 * @Date: Created in 20:26 2018/4/11
 * @Modified By:
 */
@Service("accountServe")
public class AccountServe {
    @Autowired
    CompanyRepository companyRepository;
    @Autowired
    TalentRepository talentRepository;
    @Autowired
    private JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String username;
//  找回密码
    public void findPasswordInComputer(String code) throws Exception {
        List<Company> list = companyRepository.findByCode(code);

        if(list.size()>0){
            Company company = list.get(0);
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(username);
            message.setTo(company.getEmail());
            message.setSubject("密码找回");
            message.setText("原密码为："+company.getPassword());
            javaMailSender.send(message);
        }else {
            throw new Exception("企业账号不存在");
        }
    }

    public void findPasswordInTalent(String code) throws Exception {
        List<Talent> list = talentRepository.findByCode(code);

        if(list.size()>0){
            Talent talent = list.get(0);
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(username);
            message.setTo(talent.getEmail());
            message.setSubject("密码找回");
            message.setText("原密码为："+talent.getPassword());
            javaMailSender.send(message);
        }else {
            throw new Exception("求职者账号不存在");
        }
    }
//  修改密码
    public void editCompanyPassword(String code,String password,String newPassword) throws Exception {
        List<Company> list = companyRepository.findByCode(code);
        if(list.size()>0){
            Company company = list.get(0);
            if(company.getPassword().equals(password)){
                company.setPassword(newPassword);
                companyRepository.save(company);
            }else {
                throw new Exception("原密码错误！");
            }
        }
    }

    public void editTalentPassword(String code,String password,String newPassword) throws Exception {
        List<Talent> list = talentRepository.findByCode(code);
        if(list.size()>0){
            Talent talent = list.get(0);
            if(talent.getPassword().equals(password)){
                talent.setPassword(newPassword);
                talentRepository.save(talent);
            }else {
                throw new Exception("原密码错误！");
            }
        }
    }
//  查找信息
    public Company findInfoCompany(String code) throws Exception {
        List<Company> list = companyRepository.findByCode(code);
        if(list.size()>0){
            Company company = list.get(0);
            return company;
        }
        throw new Exception("账号信息不存在");
    }

    public Talent findInfoTalent(String code) throws Exception {
        List<Talent> list = talentRepository.findByCode(code);
        if(list.size()>0){
            Talent talent = list.get(0);
            return talent;
        }
        throw new Exception("账号信息不存在");
    }
//  修改资料
    public void editCompany(Company company) throws Exception {
        //重复验证
        List<Company> list = companyRepository.findByName(company.getName());
        if (list.size() > 0 && list.get(0).getId()!= company.getId()){
            throw new Exception("企业名称不能重复！");
        }
        list = companyRepository.findByEmail(company.getEmail());
        if (list.size()> 0 && list.get(0).getId()!= company.getId()){
            throw new Exception("企业邮箱不能重复！");
        }

        companyRepository.save(company);
    }

    public void editTalent(Talent talent) throws Exception {
        //重复验证
        List<Talent> list = talentRepository.findByEmail(talent.getEmail());
        if (list.size()> 0 && list.get(0).getId()!= talent.getId()){
            throw new Exception("求职者邮箱不能重复！");
        }

        talentRepository.save(talent);
    }
}
